{% extends "../../main.html" %}

{% block title %}{{_("Source Code Market")}}{% end %}

{% block header %}
<script src="/static/js/pages/admin/upgrades/source_code_market.js"></script>
{% from tornado.options import options %}
{% end %}

{% block modals %}
<div id="add-source-code-modal" class="modal hide fade" style="display: none; ">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h3>{{_("Add Source Code to Market")}}</h3>
    </div>
    <div class="modal-body">
        <form id="add-source-code-form" class="form-horizontal" action="/admin/upgrades/source_code_market/add" method="post" enctype="multipart/form-data">
            {% raw xsrf_form_html() %}
            <input id="add-source-code-uuid" type="hidden" name="box_uuid" value="" />
            <div class="control-group">
                <label class="control-label" for="price">{% if options.banking %}{{ _("Price") }}{% else %}{{ _("Cost") }}{% end %}</label>
                <div class="controls">
                    <input autofocus required id="price" name="price" type="text" pattern="[0-9]+" placeholder="{% if options.banking %}{{ _('Price') }} ({{_('$')}}){% else %}{{ _('Cost') }}{% end %}">
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="description">{{_("Description")}}</label>
                <div class="controls">
                    <input required id="description" name="description" type="text" placeholder="{{_('Description')}}">
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="source_archive">{{_("Source Code Archive")}}</label>
                <div class="controls">
                    <input id="source_archive" name="source_archive" type="file">
                </div>
            </div>
        </form>
    </div>
    <div class="modal-footer">
        <a href="#" class="btn" data-dismiss="modal">{{_("Close")}}</a>
        <button id="add-source-code-submit" class="btn btn-primary" type="button">
            {{_("Add to Market")}}
        </button>
    </div>
</div>
<div id="delete-source-code-modal" class="modal hide fade" style="display: none; ">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h3>
            <i class="fa fa-times-circle-o"></i>
            {{_("Delete Source Code")}}
        </h3>
    </div>
    <div class="modal-body">
        <p>
            {{_("Are you sure you want to delete this source code?")}}
            <br />
        </p>
        <form id="delete-source-code-form" method="post" action="/admin/upgrades/source_code_market/delete">
            {% raw xsrf_form_html() %}
            <input id="delete-source-code-uuid" name="box_uuid" type="hidden" value="" />
        </form>
    </div>
    <div class="modal-footer">
        <a href="#" class="btn" data-dismiss="modal">{{_("Close")}}</a>
        <button id="delete-source-code-submit" type="button" class="btn btn-danger">
            <i class="fa fa-times-circle-o"></i>
            {{_("Delete")}}
        </button>
    </div>
</div>
{% end %}

{% block content %}
{% from models.Box import Box %}
<div class="container">
    <h1>
        <i class="fa fa-code"></i>
        {{_("Source Code Market")}}
    </h1>
    <br />
    {% if errors is not None and len(errors) != 0 %}
        {% for error in errors %}
        <div class="alert alert-error">
            <a class="close" data-dismiss="alert" href="#">&times;</a>
            <h4 class="alert-heading">{{_("ERROR")}}</h4>
            {{ error }}
        </div>
        {% end %}
    {% end %}
    <div class="well">
        {% if len(Box.unlocked()) == 0 %}
            <p><strong>{{_("No Boxes")}}</strong></p>
        {% else %}
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>{{_("Box")}}</th>
                        <th>{{_("File Name")}}</th>
                        <th>{{_("Description")}}</th>
                        <th>{% if options.banking %}{{ _("Price") }}{% else %}{{ _("Cost") }}{% end %}</th>
                        <th>
                            <!-- Add/Delete Button -->
                        </th>
                    </tr>
                </thead>
                <tbody>
                    {% for index, box in enumerate(Box.all()) %}
                    <tr>
                        <td>{{ index + 1 }}</td>
                        <td>{{ box.name }}</td>
                        {% if box.source_code is None %}
                        <td>N / A</td>
                        <td>N / A</td>
                        <td>N / A</td>
                        <td>
                            <a id="add-source-code-button{{ index }}" class="btn btn-mini" data-toggle="modal" href="#add-source-code-modal" data-uuid="{{ box.uuid }}">
                                {{_("Add Source Code")}}
                            </a>
                        </td>
                        {% else %}
                        <td>{{ box.source_code.file_name }}</td>
                        <td>{{ box.source_code.description }}</td>
                        <td>{% if options.banking %}{{ _("$") }}{% end %}{{ box.source_code.price }}</td>
                        <td>
                            <a id="delete-source-code-button{{ index }}" class="btn btn-mini btn-danger" data-toggle="modal" href="#delete-source-code-modal" data-uuid="{{ box.uuid }}">
                                <i class="fa fa-times-circle-o"></i>
                                {{_("Delete Source Code")}}
                            </a>
                        </td>
                        {% end %}
                    </tr>
                    {% end %}
                </tbody>
            </table>
        {% end %}
    </div>
</div>
{% end %}
